# -*- coding: utf-8 -*-
# @Time    : 2020/7/15 10:15
# @Author  : ZhaoHexin
# @File    : SsoCreateUi.py
# @Description  :sso制作工具UI页面

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QIntValidator, QRegExpValidator
from PyQt5.QtWidgets import QLineEdit, QMessageBox

from config import InfoConfig


class MyQLine(QLineEdit):
    """实现文件拖放功能"""

    def __init__(self):
        super().__init__()
        self.setAcceptDrops(True)  # 设置接受拖放动作

    def dragEnterEvent(self, e):
        # 循环判断当前页签值，并锁定需要的文件格式
        print(InfoConfig.currentIndex)
        if InfoConfig.currentIndex == 1:
            if e.mimeData().text().endswith('.xml'):  # 如果是.xml结尾的路径接受
                e.accept()
            else:
                QMessageBox.warning(self, "警告", self.tr("只接受.xml格式的文件哦！"))
        elif InfoConfig.currentIndex == 2:
            if e.mimeData().text().endswith('.csv'):  # 如果是.csv结尾的路径接受
                e.accept()
            else:
                QMessageBox.warning(self, "警告", self.tr("只接受.csv格式的文件哦！"))

    def dropEvent(self, e):  # 放下文件后的动作
        path = e.mimeData().text().replace('file:///', '')  # 删除多余开头
        self.setText(path)


class UiSsoCreate(object):
    def setupUi(self, SsoCreate):
        SsoCreate.setObjectName("SsoCreate")
        SsoCreate.resize(700, 650)
        self.gridlayout = QtWidgets.QGridLayout(SsoCreate)
        self.gridlayout.setContentsMargins(9, 9, 9, 9)
        self.gridlayout.setSpacing(6)
        self.gridlayout.setObjectName("gridlayout")
        spacerItem = QtWidgets.QSpacerItem(700, 0, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridlayout.addItem(spacerItem, 2, 0, 1, 1)
        self.tabWidget = QtWidgets.QTabWidget(SsoCreate)
        self.tabWidget.setObjectName("tabWidget")
        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab")
        self.hboxlayout = QtWidgets.QGridLayout(self.tab)
        self.hboxlayout.setContentsMargins(9, 9, 9, 9)
        self.hboxlayout.setSpacing(6)
        self.hboxlayout.setObjectName("hboxlayout")
        self.textBrowser = QtWidgets.QTextBrowser(SsoCreate)
        self.textBrowser.setObjectName("textBrowser")
        self.hboxlayout.addWidget(self.textBrowser, 1, 1, 100, 100)
        self.tabWidget.addTab(self.tab, "")
        self.tab_2 = QtWidgets.QWidget()
        self.tab_2.setObjectName("tab_2")
        self.gridlayout1 = QtWidgets.QGridLayout(self.tab_2)
        self.gridlayout1.setContentsMargins(9, 9, 9, 9)
        self.gridlayout1.setSpacing(6)
        self.gridlayout1.setObjectName("gridlayout1")
        self.tabWidget.addTab(self.tab_2, "")
        self.tab_3 = QtWidgets.QWidget()
        self.tab_3.setObjectName("tab_3")
        self.gridlayout2 = QtWidgets.QGridLayout(self.tab_3)
        self.gridlayout2.setContentsMargins(9, 9, 9, 9)
        self.gridlayout2.setSpacing(9)
        self.gridlayout2.setObjectName("gridlayout2")
        self.tabWidget.addTab(self.tab_3, "")
        self.gridlayout.addWidget(self.tabWidget, 1, 0, 1, 1)
        self.gridlayout3 = QtWidgets.QGridLayout()
        self.gridlayout3.setContentsMargins(9, 9, 9, 9)
        self.gridlayout3.setSpacing(6)
        self.gridlayout3.setObjectName("gridlayout3")
        self.label_3 = QtWidgets.QLabel(SsoCreate)
        self.label_3.setObjectName("label_3")
        self.gridlayout3.addWidget(self.label_3, 0, 2, 1, 1)
        self.label = QtWidgets.QLabel(SsoCreate)
        self.label.setObjectName("label")
        self.gridlayout3.addWidget(self.label, 0, 0, 1, 1)
        self.label_5 = QtWidgets.QLabel(SsoCreate)
        self.label_5.setObjectName("label_5")
        self.gridlayout3.addWidget(self.label_5, 1, 0, 1, 1)
        self.label_12 = QtWidgets.QLabel(SsoCreate)
        self.label_12.setObjectName("label_12")
        self.gridlayout3.addWidget(self.label_12, 2, 0, 1, 1)
        self.surnameLineEdit = QtWidgets.QLineEdit(SsoCreate)
        self.surnameLineEdit.setObjectName("surnameLineEdit")
        self.gridlayout3.addWidget(self.surnameLineEdit, 0, 1, 1, 1)
        self.forenameLineEdit = QtWidgets.QLineEdit(SsoCreate)
        self.forenameLineEdit.setObjectName("forenameLineEdit")
        self.gridlayout3.addWidget(self.forenameLineEdit, 0, 3, 1, 1)
        self.identityLineEdit = QtWidgets.QLineEdit(SsoCreate)
        self.identityLineEdit.setObjectName("identityLineEdit")
        self.gridlayout3.addWidget(self.identityLineEdit, 1, 1, 1, 1)
        self.majorLineEdit = QtWidgets.QLineEdit(SsoCreate)
        self.majorLineEdit.setObjectName("majorLineEdit")
        self.gridlayout3.addWidget(self.majorLineEdit, 1, 3, 1, 1)
        self.pathLineEdit = QtWidgets.QLineEdit(SsoCreate)
        self.pathLineEdit.setObjectName("pathLineEdit")
        self.gridlayout3.addWidget(self.pathLineEdit, 2, 1, 1, 3)
        self.label_4 = QtWidgets.QLabel(SsoCreate)
        self.label_4.setObjectName("label_4")
        self.gridlayout3.addWidget(self.label_4, 1, 2, 1, 1)
        self.gridlayout.addLayout(self.gridlayout3, 0, 0, 1, 1)
        self.label_3.setBuddy(self.surnameLineEdit)
        self.label.setBuddy(self.forenameLineEdit)
        self.label_4.setBuddy(self.identityLineEdit)
        self.label_5.setBuddy(self.majorLineEdit)
        self.label_12.setBuddy(self.pathLineEdit)
        self.pushButton = QtWidgets.QPushButton(SsoCreate)
        self.pushButton.setObjectName("pushButton")
        self.gridlayout3.addWidget(self.pushButton, 2, 4, 1, 1)

        self.pushButton_encryption = QtWidgets.QPushButton(SsoCreate)
        self.pushButton_encryption.setObjectName("pushButton_encryption")
        self.gridlayout.addWidget(self.pushButton_encryption)
        self.pushButton_decode = QtWidgets.QPushButton(SsoCreate)
        self.pushButton_decode.setObjectName("pushButton_decode")
        self.gridlayout.addWidget(self.pushButton_decode)

        self.paidCheckBox = QtWidgets.QCheckBox(self.tab)
        self.paidCheckBox.setObjectName("paidCheckBox")
        self.gridlayout1.addWidget(self.paidCheckBox, 2, 1, 1, 1)

        self.textBrowser_1 = QtWidgets.QTextBrowser(SsoCreate)
        self.textBrowser_1.setObjectName("textBrowser_1")
        self.gridlayout1.addWidget(self.textBrowser_1, 3, 1, 1, 3)

        self.label_13 = QtWidgets.QLabel(SsoCreate)
        self.label_13.setObjectName("label_13")
        self.gridlayout1.addWidget(self.label_13, 1, 1, 1, 1)

        self.textBrowser_2 = QtWidgets.QTextBrowser(SsoCreate)
        self.textBrowser_2.setObjectName("textBrowser_2")
        self.gridlayout2.addWidget(self.textBrowser_2, 3, 1, 1, 3)

        self.label_14 = QtWidgets.QLabel(SsoCreate)
        self.label_14.setObjectName("label_14")
        self.gridlayout2.addWidget(self.label_14, 1, 1, 1, 1)

        self.label_15 = QtWidgets.QLabel(SsoCreate)
        self.label_15.setObjectName("label_15")
        self.gridlayout3.addWidget(self.label_15, 4, 0, 1, 1)

        self.pathEdit = MyQLine()
        self.pathEdit.setObjectName("declassifiedPathEdit")
        self.gridlayout1.addWidget(self.pathEdit, 1, 2, 1, 1)
        self.pathEdit.setAcceptDrops(True)

        self.registrationCodeEdit = QtWidgets.QLineEdit(SsoCreate)
        self.registrationCodeEdit.setObjectName("registrationCodeEdit")
        self.gridlayout3.addWidget(self.registrationCodeEdit, 4, 1, 1, 1)

        self.declassifiedsPathEdit = MyQLine()
        self.declassifiedsPathEdit.setObjectName("declassifiedsPathEdit")
        self.gridlayout2.addWidget(self.declassifiedsPathEdit, 1, 2, 1, 1)
        self.declassifiedsPathEdit.setAcceptDrops(True)

        self.resetButton = QtWidgets.QPushButton(SsoCreate)
        self.pushButton.setObjectName("resetButton")
        self.gridlayout3.addWidget(self.resetButton, 4, 2, 1, 1)


        self.label_16 = QtWidgets.QLabel(SsoCreate)
        self.label_16.setObjectName("label_16")
        self.gridlayout2.addWidget(self.label_16, 2, 1, 1, 1)

        self.progressBar = QtWidgets.QProgressBar(SsoCreate)
        self.progressBar.setObjectName("progressBar")
        self.gridlayout2.addWidget(self.progressBar, 2, 2, 1, 1)

        self.retranslateUi(SsoCreate)
        self.tabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(SsoCreate)

    def retranslateUi(self, SsoCreate):
        _translate = QtCore.QCoreApplication.translate
        SsoCreate.setWindowTitle(_translate("SsoCreate", "sso文件转换"))
        self.textBrowser.setText(_translate("SsoCreate", "此页签为单个创建sso功能"))
        self.textBrowser.append(_translate("SsoCreate", ""))
        self.textBrowser.append(_translate("SsoCreate", "请依次输入用户名/密码/专业标识/专业标识中文名称并点击“加密/创建按钮”"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("SsoCreate", "创建sso文件"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("SsoCreate", "加密/解密文件"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("SsoCreate", "批量创建sso文件"))
        self.label_3.setText(_translate("SsoCreate", "&*密码:"))
        self.label.setText(_translate("SsoCreate", "&*用户名:"))
        self.label_5.setText(_translate("SsoCreate", "&*专业标识:"))
        self.label_4.setText(_translate("SsoCreate", "&专业中文名:"))
        self.label_12.setText(_translate("SsoCreate", "&存储路径:"))
        self.pushButton.setText(_translate("SsoCreate", "选择"))
        self.pushButton_encryption.setText(_translate("SsoCreate", "加密/创建"))
        self.pushButton_decode.setText(_translate("SsoCreate", "解密"))

        self.paidCheckBox.setText(_translate("SsoCreate", "是否生成解密文件"))
        self.label_13.setText(_translate("SsoCreate", "待加/解密文件："))
        self.label_14.setText(_translate("SsoCreate", "待批量加密文件："))
        self.label_15.setText(_translate("SsoCreate", "*注册码："))
        self.label_16.setText(_translate("SsoCreate", "生成进度："))

        self.pushButton_decode.setEnabled(False)
        self.pushButton_encryption.setEnabled(True)

        self.pathLineEdit.setReadOnly(True)

        reg = QRegExp('[0-9；;]+$')
        # 自定义文本验证器
        pValidator = QRegExpValidator(self)
        # 设置属性
        pValidator.setRegExp(reg)
        self.registrationCodeEdit.setValidator(pValidator)

        self.surnameLineEdit.setText("thtf")
        self.forenameLineEdit.setText("a")

        self.textBrowser_1.setText(_translate("SsoCreate", "此页签为以文件形式进行加密/解密处理"))
        self.textBrowser_1.append(_translate("SsoCreate", ""))
        self.textBrowser_1.append(_translate("SsoCreate", "不需要输入上述基本信息，只支持.xml格式文件"))
        self.textBrowser_1.append(_translate("SsoCreate", ""))
        self.textBrowser_1.append(
            _translate("SsoCreate", "是否生成解密文件，若勾选则在所选目录生成mdm_auto_account_decode.xml解密文件，若不勾选，只展示解密数据"))
        self.textBrowser_1.append(_translate("SsoCreate", ""))

        self.textBrowser_2.setText(_translate("SsoCreate", "此页签为批量制作sso文件，支持单文件内多专业"))
        self.textBrowser_2.append(_translate("SsoCreate", ""))
        self.textBrowser_2.append(_translate("SsoCreate", "不需要注册码，注册码以文件内为主，只支持.csv格式文件"))
        self.textBrowser_2.append(_translate("SsoCreate", ""))
        self.textBrowser_2.append(_translate("SsoCreate", "csv文件格式如下："))
        self.textBrowser_2.append(_translate("SsoCreate", "path,regioncode,username,password"))
        self.textBrowser_2.append(_translate("SsoCreate",
                                             "sso/2019ydldldc_00GJ/6608075115150,6608075115150,user0,a"))
        self.textBrowser_2.append(_translate("SsoCreate", "注解：一定要有标题，否则少一行数据"))
        self.textBrowser_2.append(_translate("SsoCreate", "path: 该sso文件存储路径"))
        self.textBrowser_2.append(_translate("SsoCreate", "regioncode: 该sso文件注册码"))
        self.textBrowser_2.append(_translate("SsoCreate", "username: 该sso文件用户名，若为空，则使用上方所配置的用户名"))
        self.textBrowser_2.append(_translate("SsoCreate", "password: 该sso文件密码，若为空，则使用上方所配置的密码"))
        self.textBrowser_2.append(_translate("SsoCreate", ""))

        self.resetButton.setText(_translate("SsoCreate", "重置历史输入"))

        # self.surnameLineEdit.setClearButtonEnabled(True)
        # self.forenameLineEdit.setClearButtonEnabled(True)
        # self.identityLineEdit.setClearButtonEnabled(True)
        # self.majorLineEdit.setClearButtonEnabled(True)
        # self.registrationCodeEdit.setClearButtonEnabled(True)
        # self.pathEdit.setClearButtonEnabled(True)
        # self.declassifiedsPathEdit.setClearButtonEnabled(True)
